package hhh.app.presenter.ui.base;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import hhh.app.R;
import hhh.app.common.utils.T;
import hhh.app.presenter.App;
import hhh.app.presenter.di.component.AppComponent;
import hhh.app.presenter.model.productlist.ProductListActivity;


/**
 * Created by hhh on 2016/6/12.
 */
public abstract class BaseActivity extends AppCompatActivity implements IView {

    private boolean isExit;
    private Handler handler=new Handler();
    private ProgressDialog loading;
    private Unbinder mUnbinder;

//    protected P mPrensenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResId());
        mUnbinder= ButterKnife.bind(this);

        this.getAppComponent().inject(this);
//        createPresenter();
//        if(mPrensenter!=null){
//            mPrensenter.attachView(this);
//        }
        App.getApp().put(getClass().getName(),this);

        init();
    }

    protected abstract void init();

    protected abstract int getLayoutResId();

    public AppComponent getAppComponent(){
        return ((App)getApplication()).getAppComponent();
    }

    protected void addFragment(int containerViewId, Fragment fragment){
        FragmentTransaction transaction=this.getSupportFragmentManager().beginTransaction();
//        transaction.add(containerViewId,fragment);
        transaction.replace(containerViewId,fragment);
        transaction.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String msg="";
        switch (item.getItemId()){
            case R.id.menu_about:
                break;
            case R.id.menu_quit:
                break;
            case R.id.menu_settings:
                break;
        }
        Snackbar snackbar=Snackbar.make(getCurrentFocus(),msg,Snackbar.LENGTH_LONG);
        snackbar.setAction("OK", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        snackbar.setActionTextColor(Color.BLUE);
        snackbar.show();
        return true;
    }

    //    protected abstract void createPresenter();

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            if(getClass().getName().equals(ProductListActivity.class.getName())){
                if(isExit){
                    App.getApp().closeAll();
                }else{
                    isExit=true;
                    T.show(this, R.string.exit);
                    handler.postDelayed(r,2000);
                }
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private Runnable r=new Runnable() {
        @Override
        public void run() {
            isExit=false;
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
/*        if(mPrensenter!=null){
            mPrensenter.detachView();
        }*/
        mUnbinder.unbind();
        App.getApp().remove(getClass().getName());
    }

    public void showLoading(){
        if(loading==null){
            //loading=new AlertDialog.Builder(this).create();
            loading=new ProgressDialog(this);
            loading.setMessage("加载中");
//            loading.setCanceledOnTouchOutside(false);
//            Window w=loading.getWindow();
//            w.setWord(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
        loading.show();
    }

    public void dismissLoading(){
        loading.dismiss();
    }
}
